package com.smarthome.ipc.hk.demo;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.swing.tree.DefaultMutableTreeNode;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.smarthome.domain.ipc.IpcCodeEnum;
import com.smarthome.domain.ipc.IpcConstants;
import com.smarthome.domain.ipc.req.IpcLogRecordRequest;
import com.smarthome.ipc.IpcClientMain;
import com.smarthome.ipc.hk.demo.HCNetSDK;
import com.smarthome.ipc.hk.demo.JFramePTZControl;
import com.smarthome.ipc.hk.demo.ClientDemo.FMSGCallBack;
import com.smarthome.ipc.hk.demo.ClientDemo.FRealDataCallBack;
import com.sun.jna.NativeLong;
import com.sun.jna.ptr.NativeLongByReference;

@Service
public class HkClientService {
	private static final Logger LOGGER = LoggerFactory.getLogger(HkClientService.class);
	private final static String PRE_LOGGER = "[HkClientService]";
	private HCNetSDK hCNetSDK = HCNetSDK.INSTANCE;
	private PlayCtrl playControl = PlayCtrl.INSTANCE;
	private NativeLong g_lVoiceHandle;// 全局的语音对讲句柄

	private boolean bRealPlay;// 是否在预览.
	private String m_sDeviceIP;// 已登录设备的IP地址

	private Map<NativeLong, Object> lUserIDs = new HashMap<NativeLong, Object>();// 用户集句柄
	private NativeLong lPreviewHandle;// 预览句柄
	private NativeLongByReference m_lPort;// 回调预览时播放库端口指针

	private NativeLong lAlarmHandle;// 报警布防句柄
	private NativeLong lListenHandle;// 报警监听句柄

	private FMSGCallBack fMSFCallBack;// 报警回调函数实现
	private FRealDataCallBack fRealDataCallBack;// 预览回调函数实现

	private JFramePTZControl framePTZControl;// 云台控制窗口

	private int m_iTreeNodeNum;// 通道树节点数目
	private DefaultMutableTreeNode m_DeviceRoot;// 通道树根节点

	@PostConstruct
	public boolean hkinit() {
		boolean initSuc = hCNetSDK.NET_DVR_Init();
		if (initSuc != true) {
			LOGGER.error(PRE_LOGGER + "初始化失败,错误码为[" + hCNetSDK.NET_DVR_GetLastError() + "].");
			IpcLogRecordRequest requestrtn = new IpcLogRecordRequest();
			requestrtn.setCommand(IpcConstants.IPC_COMMAND_LOG_RECORD);
			requestrtn.setCmd("hCNetSDK.NET_DVR_Init");
			requestrtn.setContent("海康门口机SDK初始化失败,错误码为[" + hCNetSDK.NET_DVR_GetLastError() + "].");
			requestrtn.setRemark("海康门口机SDK初始化失败.");
			requestrtn.setFailTimes(0);
			requestrtn.setSn("");
			requestrtn.setRtnCode(IpcCodeEnum.SYSTEM_ERROR.getCode());

			try {
				IpcClientMain.getInstance().sendCommand(requestrtn);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				LOGGER.info(PRE_LOGGER + "保持心跳失败后，向云平台推送错误日志产生异常！" + e1.getMessage());
			}
		}
		return initSuc;
	}

	@PreDestroy
	public void hkcleanup() {
		// 如果在预览,先停止预览, 释放句柄
		if (lPreviewHandle.longValue() > -1) {
			hCNetSDK.NET_DVR_StopRealPlay(lPreviewHandle);
			if (framePTZControl != null) {
				framePTZControl.dispose();
			}
		}

		// 报警撤防
		if (lAlarmHandle.intValue() != -1) {
			hCNetSDK.NET_DVR_CloseAlarmChan_V30(lAlarmHandle);
		}
		// 停止监听
		if (lListenHandle.intValue() != -1) {
			hCNetSDK.NET_DVR_StopListen_V30(lListenHandle);
		}

		// 如果已经注册,注销
		for (NativeLong lUserID : lUserIDs.keySet()) {
			if (lUserID.longValue() > -1) {
				hCNetSDK.NET_DVR_Logout_V30(lUserID);
			}
		}
		// cleanup SDK
		boolean initSuc = hCNetSDK.NET_DVR_Cleanup();
		if (initSuc != true) {
			LOGGER.error(PRE_LOGGER + "释放资源失败,错误码为[" + hCNetSDK.NET_DVR_GetLastError() + "].");
			IpcLogRecordRequest requestrtn = new IpcLogRecordRequest();
			requestrtn.setCommand(IpcConstants.IPC_COMMAND_LOG_RECORD);
			requestrtn.setCmd("hCNetSDK.NET_DVR_Cleanup");
			requestrtn.setContent("海康门口机SDK释放资源失败,错误码为[" + hCNetSDK.NET_DVR_GetLastError() + "].");
			requestrtn.setRemark("海康门口机SDK释放资源失败.");
			requestrtn.setFailTimes(0);
			requestrtn.setSn("");
			requestrtn.setRtnCode(IpcCodeEnum.SYSTEM_ERROR.getCode());
			try {
				IpcClientMain.getInstance().sendCommand(requestrtn);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				LOGGER.info(PRE_LOGGER + "保持心跳失败后，向云平台推送错误日志产生异常！" + e1.getMessage());
			}

		}

	}

	public HCNetSDK gethCNetSDK() {
		return hCNetSDK;
	}

	public void sethCNetSDK(HCNetSDK hCNetSDK) {
		this.hCNetSDK = hCNetSDK;
	}

	public PlayCtrl getPlayControl() {
		return playControl;
	}

	public void setPlayControl(PlayCtrl playControl) {
		this.playControl = playControl;
	}

	public NativeLong getG_lVoiceHandle() {
		return g_lVoiceHandle;
	}

	public void setG_lVoiceHandle(NativeLong g_lVoiceHandle) {
		this.g_lVoiceHandle = g_lVoiceHandle;
	}

	public boolean isbRealPlay() {
		return bRealPlay;
	}

	public void setbRealPlay(boolean bRealPlay) {
		this.bRealPlay = bRealPlay;
	}

	public String getM_sDeviceIP() {
		return m_sDeviceIP;
	}

	public void setM_sDeviceIP(String m_sDeviceIP) {
		this.m_sDeviceIP = m_sDeviceIP;
	}

	public Map<NativeLong, Object> getlUserIDs() {
		return lUserIDs;
	}

	public void setlUserIDs(Map<NativeLong, Object> lUserIDs) {
		this.lUserIDs = lUserIDs;
	}

	public NativeLong getlPreviewHandle() {
		return lPreviewHandle;
	}

	public void setlPreviewHandle(NativeLong lPreviewHandle) {
		this.lPreviewHandle = lPreviewHandle;
	}

	public NativeLongByReference getM_lPort() {
		return m_lPort;
	}

	public void setM_lPort(NativeLongByReference m_lPort) {
		this.m_lPort = m_lPort;
	}

	public NativeLong getlAlarmHandle() {
		return lAlarmHandle;
	}

	public void setlAlarmHandle(NativeLong lAlarmHandle) {
		this.lAlarmHandle = lAlarmHandle;
	}

	public NativeLong getlListenHandle() {
		return lListenHandle;
	}

	public void setlListenHandle(NativeLong lListenHandle) {
		this.lListenHandle = lListenHandle;
	}

	public FMSGCallBack getfMSFCallBack() {
		return fMSFCallBack;
	}

	public void setfMSFCallBack(FMSGCallBack fMSFCallBack) {
		this.fMSFCallBack = fMSFCallBack;
	}

	public FRealDataCallBack getfRealDataCallBack() {
		return fRealDataCallBack;
	}

	public void setfRealDataCallBack(FRealDataCallBack fRealDataCallBack) {
		this.fRealDataCallBack = fRealDataCallBack;
	}

	public JFramePTZControl getFramePTZControl() {
		return framePTZControl;
	}

	public void setFramePTZControl(JFramePTZControl framePTZControl) {
		this.framePTZControl = framePTZControl;
	}

	public int getM_iTreeNodeNum() {
		return m_iTreeNodeNum;
	}

	public void setM_iTreeNodeNum(int m_iTreeNodeNum) {
		this.m_iTreeNodeNum = m_iTreeNodeNum;
	}

	public DefaultMutableTreeNode getM_DeviceRoot() {
		return m_DeviceRoot;
	}

	public void setM_DeviceRoot(DefaultMutableTreeNode m_DeviceRoot) {
		this.m_DeviceRoot = m_DeviceRoot;
	}

}
